home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10540 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: surfnet.nl!sun4nl!ittpub!ittpub!nntp
  2. Newsgroups: comp.lang.c++
  3. Subject: Re: Operator overloading
  4. Message-ID: <1996Mar8.163132.1795@ittpub>
  5. From: wil@ittpub.nl (Wil Evers)
  6. Date: 8 Mar 96 16:31:32 WET
  7. References: <313F19B5.41C6@lfa.uni-wuppertal.de>
  8. Distribution: world
  9. Nntp-Posting-Host: lintilla
  10.  
  11. In article <313F19B5.41C6@lfa.uni-wuppertal.de> Oliver Heinz  
  12. <heinz@lfa.uni-wuppertal.de> writes:
  13.  
  14. > Hello out there, 
  15. > can somebody explain to me this strange looking operator overloading:
  16. > const T& operator[](unsigned long int index) const;
  17. > I know, it is an subscription overloading, for something like 
  18. > v[i], where i is of type unsigned long int and v of type T.
  19. >  
  20. > What's the meaning of the const at the end 
  21. > and the beginning of the line ?
  22. > Any help is greatly appreciated, thanks in advance ...
  23. > Oliver
  24.  
  25. Before answering your question -: I'm always amazed at the amount of  
  26. questions in this newsgroup that clearly indicate someone is programming  
  27. in C++ without reading a decent introductory book about it first. Please  
  28. believe me: C++ is just too darn complex to simply bang away in it, even  
  29. if you're a C expert... Sooner or later, the language will come back to  
  30. hunt you.
  31.  
  32. To answer your question: the first `const' means you cannot modify the  
  33. object that the returned reference is referring to. The second `const'  
  34. means this member function does not change the object it is called on, so  
  35. it can be called on objects which are declared as constant. This  
  36. combination makes perfect sense, because this member function is probably  
  37. returning a reference to something that is - at least logically - stored  
  38. in the object you call it on.
  39.  
  40. - Wil
  41.